package course

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"go-test/model"
	"go-test/usercase/impl"
	"net/http"
)

func CreateCourse(c *gin.Context, db *gorm.DB) {
	var course model.Course

	err := c.BindJSON(&course)
	if err != nil {
		return
	}

	courseImpl := impl.NewCourseImpl(db)
	result, err := courseImpl.CreateCourse(&course)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "success",
			"data": result,
		})
	}

}

func ListCourse(c *gin.Context, db *gorm.DB) {
	courseImpl := impl.NewCourseImpl(db)
	listCourse, err := courseImpl.ListCourse()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "success",
			"data": listCourse,
		})
	}
}

func DelCourse(c *gin.Context, db *gorm.DB) {
	var courseId string

	err := c.BindJSON(&courseId)
	if err != nil {
		return
	}

	courseImpl := impl.NewCourseImpl(db)
	result, err := courseImpl.DelCourse(courseId)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "success",
			"data": result,
		})
	}
}
